package com.brillilab.service.job.experiment;

import com.brillilab.service.core.experiments.IExperimentStepTimerService;
import com.brillilab.service.module.message.producer.MessageProducer;
import com.brillilab.service.module.message.template.impl.experiment.ExperimentCountdownEndTemp;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import javax.annotation.Resource;

/**
 * 计时器结束时执行，更改状态 结束-2
 */
@Component
public class TimerEndJob implements Job {

    @Resource
    private IExperimentStepTimerService experimentStepTimerService;
    @Resource
    private MessageProducer messageProducer;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        JobDataMap dataMap=context.getJobDetail().getJobDataMap();
        Long id = (Long) dataMap.get("timerId");
        Long labId = (Long) dataMap.get("labId");
        Long executorUid = (Long) dataMap.get("executorUid");
        Long executor = (Long) dataMap.get("executor");
        String experimentName = dataMap.getString("experimentName");
        String stepNo = dataMap.getString("stepNo");
        Long experimentId = (Long) dataMap.get("experimentId");
        Long phaseId = (Long) dataMap.get("phaseId");
        Long stepId = (Long) dataMap.get("stepId");

        //更改计时器状态
        Assert.notNull(id,"timerId can not be null!");
        experimentStepTimerService.endTimerById(id);

        //推送
        ExperimentCountdownEndTemp experimentCountdownEndTemp=
                new ExperimentCountdownEndTemp(labId,executorUid,executor,experimentName,stepNo,experimentId,phaseId,stepId);
        messageProducer.sendMessagePush(experimentCountdownEndTemp);

    }
}
